# Copyright 2024 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

load("@rules_platform//platform_data:defs.bzl", "platform_data")
load("@rules_python//python:defs.bzl", "py_library")
load("//pw_build:python.bzl", "pw_py_binary")

package(
    default_visibility = ["//visibility:public"],
    features = ["-layering_check"],
)

pw_py_binary(
    name = "flash",
    srcs = [
        "rp2040_utils/flasher.py",
    ],
    main = "rp2040_utils/flasher.py",
    tags = ["manual"],
    deps = [
        ":rp2040_utils",
        "@rules_python//python/runfiles",
    ],
)

py_library(
    name = "rp2040_utils",
    srcs = [
        "rp2040_utils/__init__.py",
        "rp2040_utils/device_detector.py",
        "rp2040_utils/flasher.py",
        "rp2040_utils/unit_test_client.py",
        "rp2040_utils/unit_test_server.py",
    ],
    data = [
        "@libusb//:libusb_dynamic",
        "@picotool",
        "@probe_rs//:probe-rs",
    ],
    imports = ["."],
    tags = ["manual"],
    deps = [
        "//pw_cli/py:pw_cli",
        "//pw_hdlc/py:pw_hdlc",
        "//pw_log:log_proto_py_pb2",
        "//pw_log/py:pw_log",
        "//pw_log_rpc/py:pw_log_rpc",
        "//pw_system/py:pw_system_lib",
        "//pw_tokenizer:tokenizer_proto_py_pb2",
        "//pw_tokenizer/py:pw_tokenizer",
        "//pw_unit_test:unit_test_py_pb2",
        "//pw_unit_test/py:pw_unit_test",
        "@pigweed_python_packages//pyusb",
        "@rules_python//python/runfiles",
    ],
)

pw_py_binary(
    name = "rpc_unit_test_runner",
    srcs = [
        "rp2040_utils/rpc_unit_test_runner.py",
    ],
    main = "rp2040_utils/rpc_unit_test_runner.py",
    tags = ["manual"],
    deps = [
        ":rp2040_utils",
    ],
)

pw_py_binary(
    name = "unit_test_server",
    srcs = [
        "rp2040_utils/unit_test_server.py",
    ],
    data = [
        ":rpc_unit_test_runner",
        "//pw_target_runner/go/cmd:server",
    ],
    main = "rp2040_utils/unit_test_server.py",
    tags = ["manual"],
    deps = [
        ":rp2040_utils",
        "@rules_python//python/runfiles",
    ],
)

pw_py_binary(
    name = "_unit_test_client",
    srcs = [
        "rp2040_utils/unit_test_client.py",
    ],
    data = ["//pw_target_runner/go/cmd:client"],
    main = "rp2040_utils/unit_test_client.py",
    tags = ["manual"],
    deps = [
        ":rp2040_utils",
        "@rules_python//python/runfiles",
    ],
)

platform_data(
    name = "unit_test_client",
    platform = "@bazel_tools//tools:host_platform",
    target = ":_unit_test_client",
)
